<?php

		$ano=date("Y");
        $mes=date("m");
		
		$ano2=date("Y");
		$mes2=date("m")-1;
        
        $num=$this->datasis->dameval("SELECT RIGHT((numeroi+1),5)valor FROM gser WHERE YEAR(fecha)='$ano' AND MONTH(fecha)='$mes' AND seriei='010' AND comp='L' ORDER BY numeroi DESC LIMIT 1");
		
	    $num2=$this->datasis->dameval("SELECT RIGHT((numeroi),5)valor FROM gser WHERE YEAR(fecha)='$ano2' AND MONTH(fecha)='$mes2' AND seriei='010' AND comp='L' ORDER BY numeroi DESC LIMIT 1");
		
        $num=str_pad($num,5,'0', STR_PAD_LEFT);       
        if($num=='00000' OR $num==''){
			$num='00001';
		}
		
		$num2=str_pad($num2,5,'0', STR_PAD_LEFT);       
        if($num2=='00000' OR $num2==''){
			$num2='00001';
		}
		
		$mes2=str_pad($mes2,2,'0', STR_PAD_LEFT); 
		$numerod2=$ano2.$mes2.$num2;
		$numerod=$ano.$mes.$num;
		

if ($form->_status=='delete' || $form->_action=='delete' || $form->_status=='unknow_record'):
	echo $form->output;
else:

if(isset($form->error_string)) echo '<div class="alert">'.$form->error_string.'</div>';
echo $form_scripts;
echo $form_begin;

IF($form->_status=='modify'){	
	$vmonto=0.00;
}else{
	$vmonto=$form->monto->value;
}

$link=base_url()."common/numrecibo/";

if($form->_status!='show'){ ?>

<script language="javascript" type="text/javascript">

$(function(){
	$(".inputnum").numeric(".");
	//totalizar();
});
function marcar(source,cant){
		//alert(cant);
	    totalg=0;
	    cant=cant+1;
		checkboxes=document.getElementsByTagName('input'); //obtenemos todos los controles del tipo Input
        for(i=0;i<checkboxes.length;i++) //recoremos todos los controles
        {
			
            if(checkboxes[i].type == "checkbox") //solo si es un checkbox entramos
            {
                checkboxes[i].checked=source.checked; //si es un checkbox le damos el valor del checkbox que lo llamó (Marcar/Desmarcar Todos)
                
                if(source.checked){
					
					for (var j = 0; j < cant; j++) {
						deuda = $("#deuda_"+j).val();
						pago  = $("#pago_"+j).val(deuda);
						
					}

				}else{
					
					for (var j = 0; j < cant; j++) {
						
						pago  = $("#pago_"+j).val(00);
						
					}
				}
				
				
            }
        }
        
    for (var i = 1; i < cant; i++) {
	 
     var totals = $("#pago_"+i).val();
     totals = parseFloat(totals);
     totalg += totals;
     //alert(totalg);
     
	}
	
 //intbanco   = Number($("#intbanco").val());
 //comibanco  = Number($("#comibanco").val());
  
 //tot=(totalg+intbanco)-comibanco;

  $("#vaplica").val(roundNumber(totalg,2));
  $("#vaplica_val").text(nformat(totalg,2));
       
}
function numrecibo(){
	
	seriei    = $("#serie").val();
	$.ajax({
		url: "<?=$link?>"+'/'+seriei,
		success: function(msg){
			if(msg){
				$("#numero").val(msg);
			} else {
				alert("No es posible generar un numero");
			}
		}
	});
}
function totalizar (id,count,check){
	 
 var totals=0;
 var totalg=0;
 var cant=0;
 var tot=0;
 
  
 var deuda = $("#deuda_"+id).val();
  
 var totals = $("#pago_"+id).val();
 
 var monto  = $("#vaplica").val();
 
 
 if(check==true){
	var pago= $("#pago_"+id).val(deuda);
 }else{
    var pago= $("#pago_"+id).val(0);
 }
  
  totals = parseFloat(totals);
  monto  = parseFloat(monto);
  cant   = parseInt(cant)
  cant=count+1;
 
 for (var i = 1; i < cant; i++) {
	 
     var totals = $("#pago_"+i).val();
     totals = parseFloat(totals);
     totalg += totals;
     
 }
 
 intbanco   = Number($("#intbanco").val());
 comibanco  = Number($("#comibanco").val());
  
 tot=(totalg+intbanco)-comibanco;

  $("#vaplica").val(roundNumber(totalg,2));
  $("#vaplica_val").text(nformat(totalg,2));

}
function totalizar_2 (id,count){
	
 var totals=0;
 var totalg=0;
 var cant=0;
 var tot=0;
  
 var deuda = $("#deuda_"+id).val();  
 var totals = $("#pago_"+id).val();
 var monto  = $("#vaplica").val();
  
  totals = parseFloat(totals);
  monto  = parseFloat(monto);
  cant   = parseInt(cant)
  cant=count+1;
  
  if(totals>deuda)
  alert('Pago mayor a la DEUDA PENDIENTE, Por favor verifique los montos ');
   
  //alert (cant);
 
 for (var i = 1; i < cant; i++) {
	 
     var totals = $("#pago_"+i).val();
     totals = parseFloat(totals);
     totalg += totals;
     //alert(totalg);
     
 }
 
 //intbanco   = Number($("#intbanco").val());
 //comibanco  = Number($("#comibanco").val());
  
 //tot=(totalg+intbanco)-comibanco;

  $("#vaplica").val(roundNumber(totalg,2));
  $("#vaplica_val").text(nformat(totalg,2));
 
}
</script>
<?php } ?>
<table align='center' width="95%" style='background: #EEEEEE'>
	<tr>
		<?php if ($form->_status=='show') { ?>
		<td>
		<a href="#" onClick="window.open('<?php echo base_url() ?>formatos/verhtml/ABONO/<?php echo $form->tipoe->value ?>/<?php echo $form->serie->value ?>/<?php echo $form->numero->value ?>', '_blank', 'width=800, height=600, scrollbars=Yes, status=Yes, resizable=Yes, screenx='+((screen.availWidth/2)-400)+',screeny='+((screen.availHeight/2)-300)+'');" heigth="600" >
		<img src='<?php echo base_url() ?>images/html_icon.gif'></a>
		<a href="#" onClick="window.open('<?php echo base_url() ?>formatos/ver/ABONO/<?php echo $form->tipoe->value ?>/<?php echo $form->serie->value ?>/<?php echo $form->numero->value ?>', '_blank', 'width=800, height=600, scrollbars=Yes, status=Yes, resizable=Yes, screenx='+((screen.availWidth/2)-400)+',screeny='+((screen.availHeight/2)-300)+'');" heigth="600" >
		<img src='<?php echo base_url() ?>images/pdf_logo.gif'></a> Comprobante 
		</td>
	<?php } ?>
	  <td colspan="3" align="right"><?php echo $container_tr?><?php echo $container_br?></td>
  </tr>
</table>
<table align='center' width="90%">
	<tr>
		<td>
         <table align='center' width="100%" >
	<tr>
		<td>
		<table width='100%'>
		<tr><td rowspan="5">
<fieldset style='border: 2px outset #9AC8DA;background: #FFFFFF;'>
<legend  class="titulofieldset" style='color: #2E4B77;'>Comprobante</legend>	
	<table width='100%'>
	<tr>
	<td class="littletablerow" align='right' width='40%'>Comprobante L010-DIARIOS</td>
	<td class="littletablerow" ><input name="numeroc" type="text" id="numeroc" value="<?=$numerod?>" size="12" maxlength="11" class="input"/>	  <span style='color:red'></span></td>
	</tr>
	<tr>
		<td class="littletablerow" align='right'>Fecha de Comprobante</td>
		<td class="littletablerow"><?php echo $form->fechac->output;  ?></td>
	    </tr>
	<tr>
	  <td class="littletablerow" align='right'>Diferente moneda </td>
	  <td class="littletablerow"><select class="input" id="dfmoneda" name="dfmoneda">
	    <option>N</option>
	    <option>S</option>
	    </select>	  </td>
	  </tr>
</table>
</fieldset>
		</td>
		<td width="40%">&nbsp;</td>
		</tr>
		<tr>
		  <td>&nbsp;</td>
		  </tr>
		<tr>
		  <td  class="littletableheader"><span style="color:green">Ultimo comprobante del mes
              <?= "(".$ano2."/".$mes2.")" ?>
              <?= " ".$numerod2 ?>
          </span></td>
		  </tr>
		<tr>
		  <td>&nbsp;</td>
		  </tr>
		<tr>
		  <td height="21">&nbsp;</td>
		  </tr>		
		</table>
		<br></td>
	</tr>
	<tr>
		<td>
		<div style='overflow:auto;border: 1px solid #9AC8DA;background: #FAFAFA;height:200px'>
		  <table width='90%' border='0' align="center">
            <tr class="littletablerow" align="center">
				<td colspan='9' align='left'><input type="checkbox" onclick="marcar(this,<?=$cantmd?>);" /> Marcar/Desmarcar todas las Facturas Pendientes (<?=$cantmd ?>)</td>
            </tr>
            <tr class="littletablerow" align="center">
              <td colspan="2" bgcolor='#527D9E'>Numero</td>
              <td bgcolor='#527D9E'>Letra</td>
              <td bgcolor='#527D9E'>Tipo</td>
              <td bgcolor='#527D9E'>Moneda</td>
              <td bgcolor='#527D9E'>Banco</td>
              <td bgcolor='#527D9E'>Fecha</td>
			  <td bgcolor='#527D9E'>Vence</td>
              <td bgcolor='#527D9E'>Monto</td>
              <td bgcolor='#527D9E'>Pagos Ralizados</td>
              <td bgcolor='#527D9E'>Deuda Pendiente </td>
              <td bgcolor='#527D9E'>Monto a Pagar </td>
            </tr>
		    <? $tmonto=0; $totalg=0; $tabonos=0; $tdeudas=0; $i=0; $count=count($result); foreach ($result AS $items){$i++;?>
            <? $tmonto+=$items->monto ?>
            <? $tabonos+=$items->abono ?>
            <? $tdeudas+=$items->resta ?>
            <? if($form->_status=='show'){ $pago=$items->ultiabonado; ?>
            <? }else{ $pago='0';}?>
            <? $hoy= date('Y-m-d'); ?>
            <? $vencio= $items->vence; ?>
            <? IF($vencio < $hoy){ $vence="<font color='red'>".dbdate_to_human($items->vence)."</font>"; ?>
            <? }else{ $vence=dbdate_to_human($items->vence);  }?>
            <tr>
              <td class="littletablerow" align="center">
				  <input type="checkbox"   id="chek_<?= $i ?>"  onclick="totalizar(<?= $i ?>,<?= $count ?>,this.checked)"  /></td>
              <td class="littletablerow" align="center">
				  <input type="text" name="compro[]" size=1  value="<?=$items->compro ?>"/>
				  <input type="text" name="serief[]" size=3  value="<?=$items->serief ?>"/>-
				  <input type="text"    name="factura[]"  value="<?=$items->factura?>" size="11" readonly="false" />
				  <input type="hidden" name="idp[]"   value="<?=$items->id ?>"/>
				  <input type="hidden" name="cliente[]"   value="<?=$items->cod_cli ?>"/>
				  <input type="hidden" name="tipo[]"   value="<?=$items->tipo ?>"/>
				  <input type="hidden" name="letra[]"   value="<?=$items->letra ?>"/>
				  <input type="hidden" name="cant"   value="<?=$cantmd ?>"/>
              </td>
              <td align='left' class="littletablerow"><?= $items->letra ?></td>
              <td align='left' class="littletablerow"><?= $items->tipo ?></td>
              <td align='center' class="littletablerow"><?= $items->moneda ?></td>
              <td align='center' class="littletablerow"><?= $banco=$this->datasis->dameval("SELECT descrip FROM bancos WHERE codigo='$items->banco'") ?></td>
               <td class="littletablerow" align="left"><?= dbdate_to_human($items->fecha) ?></td>
              <td align="center" class="littletablerow" ><?= $vence ?></td>
              <td class="littletablerow" align='right'><?= nformat($items->monto) ?></td>
              <td align='right' class="littletablerow"><?= nformat($items->abono) ?></td>
              <td class="littletablerow" align='right'><input id="deuda_<?= $i ?>"  name="deuda_<?= $i ?>" type="text" size="8" value="<?= $items->resta ?>" readonly="false" align="right"  /></td>
              <td class="littletablerow" align='right'><input id="pago_<?= $i ?>"   name="pago[]"  type="text" size="8" value="<?= $pago ?>" onkeyup="totalizar_2(<?= $i ?>,<?= $count ?>)" align="right" /></td>
			</tr>
            <? }?>
            <tr>
              <td colspan="7" align='right' class="littletablerow" ><strong>Totales</strong></td>
              <td align='right' class="littletablerow"><strong><? echo nformat($tmonto) ?></strong></td>
              <td align='right' class="littletablerow"><strong><? echo nformat($tabonos) ?></strong></td>
              <td class="littletablerow" align='right'><strong><? echo nformat($tdeudas) ?></strong></td>
            </tr>
          </table>
		</div>
		<?php echo $container_bl ?>		</td>
	</tr>
	<tr>
		<td>
		<fieldset style='border: 2px outset #9AC8DA;background: #EFEFEF;'>
		<legend  class="titulofieldset" style='color: #2E4B77;' align="right">-- Monto Total A PAGAR --</legend>
		<table width='100%' >
			<tr>
				<td align='right' class="littletableheader">Monto Aplicar</td>
				<td width="153" align='right' class="littletablerow"><strong><input id="vaplica"   readonly="false" name="vaplica"  type="text" size="10" value="<?= $vmonto;?>"  class="input" /></strong></td>
			</tr>
		</table>
		</fieldset>	
		</td>
	</tr>
</table>

<?php echo $form_end?>
<?php echo $container_br ?>
	  <td>
	<tr>
<table>
<?php endif; ?>
